<?php
//include_once("db.php");
include_once("Pc.php");

class ListManager implements Iterator {
	public $id;
	public $name;
	public $list;
	public $table;

	function __construct($id, $name) {
		db_init();

		$this->list = array();

		$this->name = $name;
		$this->id = $id;
		$this->table = "";

		// obtain table name using provided info
		$query = "SELECT id FROM n_wb_pc_". $this->id ." WHERE record=\"$name\"";
		$result = mysql_query($query);
		if($next = mysql_fetch_assoc($result)) {
			$table_id = $next["id"];

			$this->table = "n_wb_pc_". $this->id ."_$table_id";
		}
	}

	/***************************************************/
	/* The following are for interation. (aka foreach) */
	/***************************************************/

    public function rewind() {
		// special case, if the requested list doesn't exist
		if(strlen($this->table) == 0) {
			$this->list = array();
			return;
		}
		// normal usage:
		$query = "SELECT data, value FROM ". $this->table;
		$result = mysql_query($query);
		while($next = mysql_fetch_assoc($result)) {
			// append to list
			$this->list[$next["data"]] = $next["value"];
		}
    }
  
    public function current() {
        return current($this->list);
    }
  
    public function key() {
        return key($this->list);
    }
  
    public function next() {
        return next($this->list);
    }
  
    public function valid() {
        return (current($this->list) != null);
    }
}

?>
